home *** CD-ROM | disk | FTP | other *** search
- unit Names1u2;
-
- interface
-
- uses
- SysUtils;
-
- type
- TDataRec = packed record
- { The form's edit box has its MaxLength property set to 30 }
- Name: String[30];
- { Only interested in the date portion of this date/time value }
- DOB: TDateTime;
- end;
-
- TDataFile = class
- private
- FDataFile: File of TDataRec;
- protected
- function GetCount: Longint;
- function GetCurrent: Longint;
- function GetRecord(Index: Longint): TDataRec;
- procedure SetCurrent(RecNo: Longint);
- procedure SetRecord(Index: Longint; const DataRec: TDataRec);
- public
- constructor Create;
- destructor Destroy; override;
- property Count: Longint read GetCount;
- property Current: Longint
- read GetCurrent write SetCurrent;
- property Records[Index: Longint]: TDataRec
- read GetRecord write SetRecord; default;
- end;
-
- implementation
-
- uses
- Forms, NetLock, Consts, Classes;
-
- const
- FileName = 'DataFile.Dat';
-
- constructor TDataFile.Create;
- begin
- { Make current directory where EXE file is, just in case }
- ChDir(ExtractFilePath(Application.ExeName));
- AssignFile(FDataFile, FileName);
- FileMode := fmOpenReadWrite or fmShareDenyNone;
- try
- { Make file if it ain't there }
- if not FileExists(FileName) then
- Rewrite(FDataFile);
- Reset(FDataFile);
- except
- on E: EInOutError do
- begin
- { In case Rewrite succeeded but Reset failed }
- if TFileRec(FDataFile).Mode = fmInOut then
- CloseFile(FDataFile);
- { Customise the exception and re-raise it }
- E.Message := 'Failed to create or open ' + FileName;
- raise;
- end;
- end;
- end;
-
- destructor TDataFile.Destroy;
- begin
- if TFileRec(FDataFile).Mode = fmInOut then
- CloseFile(FDataFile);
- inherited Destroy;
- end;
-
- function TDataFile.GetCount: Longint;
- begin
- Result := FileSize(FDataFile);
- end;
-
- function TDataFile.GetCurrent: Longint;
- begin
- Result := FilePos(FDataFile);
- end;
-
- function TDataFile.GetRecord(Index: Longint): TDataRec;
- begin
- try
- Current := Index;
- Read(FDataFile, Result);
- { Go back to the beginning of the read record }
- Current := Index;
- except
- raise EListError.CreateRes(SListIndexError);
- end;
- end;
-
- procedure TDataFile.SetCurrent(RecNo: Longint);
- begin
- { Anything past EOF is considered EOF }
- if RecNo > Count then
- RecNo := Count;
- Seek(FDataFile, RecNo);
- end;
-
- procedure TDataFile.SetRecord(Index: Longint; const DataRec: TDataRec);
- var
- X: EInOutError;
- begin
- Current := Index;
- if not LockFileVar(FDataFile, Current, False) then
- begin
- X := EInOutError.Create('Cannot lock file');
- { Set up a file access denied type exception }
- X.ErrorCode := 5;
- raise X;
- end;
- try
- { DataRec is passed as a const (pass by reference, but }
- { not allowed to be treated/passed as a var parameter). }
- { We can get around this by dereferencing its }
- { address with an appropriate typecast }
- Write(FDataFile, TDataRec((@DataRec)^));
- { Go back to the beginning of the written record }
- Current := Index;
- finally
- LockFileVar(FDataFile, Current, False);
- end;
- end;
-
- end.
-